﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace AbpProjectTemplate.Utilities.Validation
{
    /// <summary>
    /// 语言验证器
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class LanguateAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null)
                return true;
            try
            {
                CultureInfo.GetCultureInfo(value.ToString());
            }
            catch
            {
                this.ErrorMessage = this.ErrorMessage ?? "{0} is invalid";
                return false;
            }

            return true;
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format(CultureInfo.CurrentCulture, this.ErrorMessageString, name);
        }
    }
}

